#include "../StdAfx.h"
#include "Window.h"

using namespace std;

/* 
 * File:   Window.cpp
 * Author: pablo
 * 
 * Created on 27 de septiembre de 2012, 12:33
 */

cWindow* cWindow::m_pGlobalWindow = NULL;

cWindow::cWindow(
        std::string title,
        int mode,
        int width= cWindow::WINDOW_WIDTH, 
        int height= cWindow::WINDOW_HEIGHT, 
        int red= cWindow::WINDOW_RED, 
        int green= cWindow::WINDOW_GREEN, 
        int blue= cWindow::WINDOW_BLUE, 
        int alpha= cWindow::WINDOW_ALPHA, 
        int bpp= cWindow::WINDOW_DEPTH, 
        int snl= cWindow::WINDOW_STENCIL
):
        m_title(title),
        m_mode(mode),
        m_Width(width),
	m_Height(height),
        m_red(red),
        m_green(green),
        m_blue(blue),
        m_alpha(alpha),
        m_bpp(bpp),
        m_snl(snl)
{
	/**
	 * Solo puede haber una ventana.
	 */
	if( m_pGlobalWindow )
	{
            printf("Ventana ya instancaiada.\n");
	}

	m_pGlobalWindow = this;
	m_bActive = false;
}

cWindow::~cWindow()
{
    glfwTerminate();
    m_pGlobalWindow = NULL;
    
    delete m_pGlobalWindow;
}

void cWindow::InitInstance() {
    // Inicializa las partes de GLFW que no dependen de una ventana (threading, timer y joystick input)
    // Se analizan posibles errores
    if (!glfwInit()) {
        // En caso de error se finaliza la aplicación
        printf("No se pudo iniciar la ventana.\n");
    }
     
    // Set properties
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
        
    // Se abre una ventana
    if (!glfwOpenWindow(this->m_Width, this->m_Height, this->m_red, this->m_green, this->m_blue, this->m_alpha, this->m_bpp, this->m_snl, this->m_mode)) {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    
    glfwSetWindowTitle(&this->m_title[0]);
}